<?PHP
unset($_SESSION['updateArray']);
?>
<script language="JavaScript">
<!--
function submitForm(event,formName){
	if(event.keyCode==13 || (event && event.which == 13)){
		confirmDelete();
	}

	//alert(event.keyCode);
}
//-->
</script>
<TABLE width="100%" border="0" cellpadding="4" cellspacing="0" style="mainTableBGColor">
	<TR><TD valign="top" align="center">

<FORM name="form" action="formaction.php" method="post" enctype="multipart/form-data" onKeyPress="submitForm(event,'form');">
		<TABLE border="1" width="625" cellpadding="2" cellspacing="0">
			<TR class="blockHeader"> <TD></TD> <TD>Program</TD> <TD>Air Time</TD> <TD>Start</TD> <TD>End</TD> <TD>Days</TD> <TD>Source</TD> <TD>Type</TD> <TD width="1%">Action</TD> </TR>


	<?PHP

		//Javascript function to confirm delete
		echo("<script language=\"JavaScript\" type=\"text/javascript\">
		function confirmDelete() {
		var cMsg = \"\";
			for (var q=0; q < document.form.elements.length; q++) {

				 // get each element of form form
				 var e = document.form.elements[q];

				 if( e.name.indexOf(\"radioGroup\") == -1 ){
				// not a radioGroup element
				continue;
  				}

				 // determine whether delete radio button is checked
				if( e.value.indexOf(\"delete\") == -1 ){
				continue;
				}


				if(e.checked == true){
				var i = e.name.replace(/radioGroup/,\"\");
				var h = document.form.elements[\"deleteID\" + i];
				cMsg += \"\\t\" + h.value + \"\\n\";
  				}



			//ends Loop
			}

		if( cMsg != \"\" ){

		  // we have some deletes to confirm

		  cMsg = \"You are about to delete the following programs:\\n\\n\" + cMsg + \"\\nContinue?\";

		  if(confirm(cMsg)){
		 document.form.submit();
		  }else{
		 return false;
		  }



		}else {
		document.form.submit();
		}


		//ends function
		}
		</script>");

		if(!isset($_SESSION['searchArray'])){


			//Set sort method according user selection
			if(isset($_GET['sort'])){
				$_SESSION['sort'] = $_GET['sort'];
			}elseif(isset($_SESSION['sort'])){
				//use existing session variable
			}else{
				$_SESSION['sort'] = 'all';
			}


			if($_SESSION['sort'] == 'all'){
				$Query = "SELECT * FROM $programTitles_Table ORDER BY military asc";
			}else{
				$Query = "SELECT * FROM $programTitles_Table WHERE type = '".$_SESSION['sort']."' ORDER BY military asc";
			}



			$result = $db->getAll($Query, DB_FETCHMODE_ASSOC);
			if (DB::isError($result)) {
				die ($result->getMessage());
			}

		}else{//for stuff coming from the search function
			$result = array();
			for ($r = 0; $r < count($_SESSION['searchArray']); $r++) {
				$sth = "SELECT * FROM $programTitles_Table WHERE ID = '".$_SESSION['searchArray'][$r]."'";
				$temp[$r] = $db->getAll($sth,DB_FETCHMODE_ASSOC);
				$result[$r] = $temp[$r][0];

			  }
			if (DB::isError($result)) {die ($result->getMessage());	}
			//echo $result[1][0]['name'];

			unset($_SESSION['searchArray']);
		}






	for ($i = 0; $i < count($result); $i++) {

	echo("<input name=\"deleteID$i\" type=\"hidden\" value=\"". stripslashes($result[$i]['title']) ."\">");//List out hidden field for javscript confirm Delete function above


	//Start Time Stamp
	if($result[$i]['startTimeStamp'] != '-1'){
		$start = date("m/d/y",intval($result[$i]['startTimeStamp']));
	}else{
		$start = "&nbsp;";
	}


	//End Time Stamp
	if($result[$i]['endTimeStamp'] != '-1'){
		$end = date("m/d/y",intval($result[$i]['endTimeStamp']));
	}else{
		$end = "&nbsp;";
	}


	//Days
	$days = "";
	if($result[$i]['mon'] == 'true'){
		$days = "&#8226;mon<BR>";
	}
	if($result[$i]['tue'] == 'true'){
		$days = $days . "&#8226;tue<BR>";
	}
	if($result[$i]['wed'] == 'true'){
		$days = $days . "&#8226;wed<BR>";
	}
	if($result[$i]['thu'] == 'true'){
		$days = $days . "&#8226;thu<BR>";
	}
	if($result[$i]['fri'] == 'true'){
		$days = $days . "&#8226;fri<BR>";
	}
	if($result[$i]['sat'] == 'true'){
		$days = $days . "&#8226;sat<BR>";
	}
	if($result[$i]['sun'] == 'true'){
		$days = $days . "&#8226;sun<BR>";
	}


	$ID = $result[$i]['ID'];
	if($result[$i]['source'] == 'Other' && $result[$i]['source_other'] != ''){
		$source = $result[$i]['source_other'];
	}else{
		$source = $result[$i]['source'];
	}

	if($result[$i]['type'] == 'Other' && $result[$i]['type_other'] != ''){
		$type = $result[$i]['type_other'];
	}else{
		$type = $result[$i]['type'];
	}

			if($i & 1){
			echo("<TR onmouseover=\"setPointer(this, $i, 'over', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#CCCCCC', '#CCFFCC', '#FFCC99');\"> <TD bgcolor=\"#CCCCCC\" class=\"numbers\">" .($i+1) .")</TD> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">" . stripslashes($result[$i]['title']) ."</TD> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\" nowrap>" . date("g",mktime ((str_replace (":", ",", $result[$i]['military'])),0,0,0)).":". substr($result[$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $result[$i]['military'])),0,0,0)) ."</TD> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">$start</td> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">$end</td> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">$days</td> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">$source</td> <TD bgcolor=\"#CCCCCC\" class=\"blockContent1\">$type</td>
			<TD bgcolor=\"#CCCCCC\" nowrap class=\"formElementTextLeft\">
				<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"formElementTextLeft\">
				<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"update:$ID\" ></TD><TD valign=\"middle\" align=\"left\">update</TD></TR>
				<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"delete:$ID\" ></TD> <TD valign=\"middle\" align=\"left\">delete</TD></TR>
				</TABLE>
			</TD></TR>\n");


			}else{
			echo("<TR onmouseover=\"setPointer(this, $i, 'over', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#FFFFFF', '#CCFFCC', '#FFCC99');\"> <TD bgcolor=\"#FFFFFF\" class=\"numbers\">" .($i+1) .")</TD> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">" . stripslashes($result[$i]['title']) ."</TD> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\" nowrap>" . date("g",mktime ((str_replace (":", ",", $result[$i]['military'])),0,0,0)).":". substr($result[$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $result[$i]['military'])),0,0,0)) ."</TD> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">$start</td> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">$end</td> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">$days</td> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">$source</td> <TD bgcolor=\"#FFFFFF\" class=\"blockContent1\">$type</td>
			<TD bgcolor=\"#FFFFFF\" nowrap class=\"formElementTextLeft\">
				<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"formElementTextLeft\">
				<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"update:$ID\" ></TD><TD valign=\"middle\" align=\"left\">update</TD></TR>
				<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"delete:$ID\" ></TD> <TD valign=\"middle\" align=\"left\">delete</TD></TR>
				</TABLE>
			</TD></TR>\n");
			}
	}

		//if($result[0]['startTimeStamp'] != "-1"){echo date("m/d/y",$result[0]['startTimeStamp']);};

		//echo date("g:i:s a",mktime ((str_replace (":", ",", $result[0]['military'])),0,0,0));


	?>



</TABLE>
<INPUT name="action" type="hidden" value="modifyPrograms"><input name="highnumber" type="hidden" value="<?PHP echo count($result); ?>"><INPUT name="Submit" type="button" class="inputButton" value="Submit" onClick ="confirmDelete()" style="margin-bottom:5px; margin-top:5px;"> &nbsp; &nbsp; &nbsp; <INPUT name="Reset" type="reset" class="inputButton" value="Reset" style="margin-bottom:5px; margin-top:5px;">
</FORM>
	</TD></TR>
</TABLE>

